{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "描述统计学（descriptive statistics）是一门统计学领域的学科，主要研究如何取得反映客观现象的数据，并以图表形式对所搜集的数据进行处理和显示，最终对数据的规律、特征做出综合性的描述分析。Pandas 库正是对描述统计学知识完美应用的体现，可以说如果没有“描述统计学”作为理论基奠，那么 Pandas 是否存在犹未可知。下列表格对 Pandas 常用的统计学函数做了简单的总结：\n",
    "\n",
    "函数名称|描述说明\n",
    ":-|:-\n",
    "count()|统计某个非空值的数量。\n",
    "sum()|求和\n",
    "mean()|求均值\n",
    "median()|求中位数\n",
    "mode()|求众数\n",
    "std()|求标准差\n",
    "min()|求最小值\n",
    "max()|求最大值\n",
    "abs()|求绝对值\n",
    "prod()|求所有数值的乘积。\n",
    "cumsum()|计算累计和，axis=0，按照行累加；axis=1，按照列累加。\n",
    "cumprod()|计算累计积，axis=0，按照行累积；axis=1，按照列累积。\n",
    "corr()|计算数列或变量之间的相关系数，取值-1到1，值越大表示关联性越强。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     name  age  score\n",
      "001    张三   20    100\n",
      "002    李四   20    102\n",
      "003    王五   20    132\n",
      "004    赵六   20    122\n",
      "005    田七   20    115\n",
      "006    刘八   20    133\n",
      "007    孙九   21    141\n",
      "008    周十   23    120\n",
      "009   吴十一   21    143\n",
      "010   郑十二   19    123\n"
     ]
    }
   ],
   "source": [
    "data = {\n",
    "    \"name\": [\"张三\", \"李四\", \"王五\", \"赵六\", \"田七\", \"刘八\", \"孙九\", \"周十\", \"吴十一\",\" 郑十二\"],\n",
    "    \"age\": [20,20,20,20,20,20,21,23,21,19],\n",
    "    \"score\": [100,102,132,122,115,133,141,120,143,123]\n",
    "}\n",
    "\n",
    "df = pd.DataFrame(data,index=[\"001\",\"002\",\"003\",\"004\",\"005\",\"006\",\"007\",\"008\",\"009\",\"010\"])\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "123.1\n",
      "----------------------------------------------\n",
      "001    120\n",
      "002    122\n",
      "003    152\n",
      "004    142\n",
      "005    135\n",
      "006    153\n",
      "007    162\n",
      "008    143\n",
      "009    164\n",
      "010    142\n",
      "dtype: int64\n"
     ]
    }
   ],
   "source": [
    "# sum() 累加\n",
    "print(df.sum(axis=0).loc['score']/10)  # axis = 0 逐行相加\n",
    "print(\"----------------------------------------------\")\n",
    "print(df.sum(axis=1)) # axis = 1 逐列相加 ，但字符型值不被计算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "age       20.4\n",
      "score    123.1\n",
      "dtype: float64\n"
     ]
    }
   ],
   "source": [
    "# mean（） 平均值\n",
    "print(df.mean())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "age       1.074968\n",
      "score    14.715449\n",
      "dtype: float64\n"
     ]
    }
   ],
   "source": [
    "# std() 求标准差\n",
    "print(df.std())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "include 相关参数值说明如下：\n",
    "- object： 表示对字符列进行统计信息描述；\n",
    "- number：表示对数字列进行统计信息描述；\n",
    "- all：汇总所有列的统计信息。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       name\n",
      "count    10\n",
      "unique   10\n",
      "top      周十\n",
      "freq      1\n",
      "------------------------------------------------------------------\n",
      "             age       score\n",
      "count  10.000000   10.000000\n",
      "mean   20.400000  123.100000\n",
      "std     1.074968   14.715449\n",
      "min    19.000000  100.000000\n",
      "25%    20.000000  116.250000\n",
      "50%    20.000000  122.500000\n",
      "75%    20.750000  132.750000\n",
      "max    23.000000  143.000000\n",
      "------------------------------------------------------------------\n",
      "       name        age       score\n",
      "count    10  10.000000   10.000000\n",
      "unique   10        NaN         NaN\n",
      "top      周十        NaN         NaN\n",
      "freq      1        NaN         NaN\n",
      "mean    NaN  20.400000  123.100000\n",
      "std     NaN   1.074968   14.715449\n",
      "min     NaN  19.000000  100.000000\n",
      "25%     NaN  20.000000  116.250000\n",
      "50%     NaN  20.000000  122.500000\n",
      "75%     NaN  20.750000  132.750000\n",
      "max     NaN  23.000000  143.000000\n"
     ]
    }
   ],
   "source": [
    "# describe()数据汇总描述, 所有描述性统计信息的大杂烩\n",
    "print(df.describe(include=\"object\"))\n",
    "print(\"------------------------------------------------------------------\")\n",
    "print(df.describe(include=\"number\"))\n",
    "print(\"------------------------------------------------------------------\")\n",
    "print(df.describe(include=\"all\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
